﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class BaseCodeRuleController : EntityController<BaseCodeRule, BaseCodeRuleSearch, IBaseCodeRuleViewModel, IBaseCodeRuleAppService>, IBaseCodeRuleController
    {
        public override string ControllerName
        {
            get { return ControllerConst.BASECODERULE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public BaseCodeRuleController()
        {
            this.Model = ModelFactory.CreateModel<IBaseCodeRuleViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "basecoderule_query")]
        public ActionResult AddBaseCodeRule(int? id)
        {
            this.Model.IsDefaultOptions = new List<SelectListItem>();
            SelectListItem sli = new SelectListItem() { Selected = true, Text = "是", Value = "1" };
            this.Model.IsDefaultOptions.Add(sli);
            //sli = new SelectListItem() { Text = "否", Value = "0" };
            //this.Model.IsDefaultOptions.Add(sli);
            this.Model.Item = new BaseCodeRule();
            this.Model.Item.SerialNumberStart = 1;
            return View("~/Views/System/v1/addBaseCodeRule.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "basecoderule_edit")]
        public JsonResult AddBaseCodeRule(BaseCodeRuleViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.IsDefault = model.IsDefault == "1" ? true : false;
            BoolGenericResult<BaseCodeRule> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "basecoderule_query")]
        public JsonResult GetBaseCodeRules(int id)
        {
            BaseCodeRuleJsonViewModel json = new BaseCodeRuleJsonViewModel();
            json.rows = new List<BaseCodeRuleJson>();

            IList<BaseCodeRule> baseCodeRules = this.AppService.GetBySearch(SearchFactory.CreateBaseCodeRuleSearch(id)).ToList();
            foreach (BaseCodeRule bcr in baseCodeRules)
            {
                BaseCodeRuleJson bcrj = new BaseCodeRuleJson();
                bcrj.Id = bcr.Id;
                bcrj.Name = bcr.Name;
                bcrj.Type = bcr.Type.GetHashCode();
                bcrj.Prefix = bcr.Prefix;
                bcrj.SerialNumberNumber = bcr.SerialNumberNumber;
                bcrj.SerialNumberStart = bcr.SerialNumberStart;
                bcrj.IsDefault = bcr.IsDefault ? "是" : "否";
                bcrj.Example = bcr.Example;
                json.rows.Add(bcrj);
            }
            json.total = baseCodeRules.Count;

            return Json(json, "application/json");
        }

        [ActionName("example")]
        [ActionPermissionAttribute(IsUsedDenied=false)]
        public JsonResult GetBaseCodeRuleExample(string pf, int snn, int sn)
        {
            BoolResult result = new BoolResult();
            StringBuilder idSB = new StringBuilder();
            idSB.Append(pf);
            idSB.Append(sn.ToString().PadLeft(snn, '0'));

            result.Messages.Add(idSB.ToString());

            return Json(result);
        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "basecoderule_edit")]
        public JsonResult DeleteBaseCodeRule(string selected)
        {
            BoolGenericResult<BaseCodeRule> result = null;
            List<BaseCodeRuleJson> baseCodeRules = JsonConvert.DeserializeObject<List<BaseCodeRuleJson>>(selected);

            foreach (BaseCodeRuleJson json in baseCodeRules)
            {
                BaseCodeRule br = this.AppService.GetById(json.Id);
                result = this.AppService.Delete(br);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
